package com.gengzp.system.controller;

import com.gengzp.common.annotations.OperationLogIgnore;
import com.gengzp.common.enums.YesNoEnum;
import com.gengzp.common.model.generic.req.SearchByIdReq;
import com.gengzp.common.model.generic.resp.Result;
import com.gengzp.common.model.pagination.resp.PaginationResponse;
import com.gengzp.common.model.task.req.CreateScheduledTaskReq;
import com.gengzp.common.model.task.req.EditScheduledTaskReq;
import com.gengzp.common.model.task.req.EditScheduledTaskStatusReq;
import com.gengzp.common.model.task.req.PageScheduledTaskReq;
import com.gengzp.common.model.task.resp.PageScheduledTaskResp;
import com.gengzp.common.model.task.resp.SearchScheduledTaskResp;
import com.gengzp.permission.annotation.AuthIgnore;
import com.gengzp.permission.annotation.CheckPermission;
import com.gengzp.system.service.ScheduledTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @ClassName ScheduledTaskController
 * @Description 定时任务控制层
 * @Author gengzp
 * @Date 2025/9/30 10:42
 */
@RestController
@RequestMapping("/task")
@Api(tags = "定时任务管理模块")
public class ScheduledTaskController {

    private static final Logger logger = org.slf4j.LoggerFactory.getLogger(ScheduledTaskController.class);

    @Autowired
    private ScheduledTaskService scheduledTaskService;

    /**
     * 创建定时任务
     */
    @PostMapping("/create")
    @ApiOperation("创建定时任务")
    @CheckPermission("task:write:create")
    public Result<Void> createScheduledTask(@Valid @RequestBody CreateScheduledTaskReq req) {
        scheduledTaskService.createScheduledTask(req);
        return Result.getSuccessResult();
    }

    /**
     * 更改定时任务状态
     */
    @PostMapping("/edit_status")
    @ApiOperation("更改定时任务开关状态")
    @CheckPermission("task:write:edit")
    public Result<Void> editScheduledTaskStatus(@Valid @RequestBody EditScheduledTaskStatusReq req) {
        if (Objects.isNull(YesNoEnum.getByCode(req.getStatus()))) {
            return Result.getFailResult(String.format("定时任务状态值编码【%s】不合法", req.getStatus()));
        }
        scheduledTaskService.editScheduledTaskStatus(req);
        return Result.getSuccessResult();
    }

    /**
     * 编辑定时任务
     */
    @PostMapping("/edit_by_id")
    @ApiOperation("根据id编辑定时任务")
    @CheckPermission("task:write:edit")
    public Result<Void> editScheduledTask(@Valid @RequestBody EditScheduledTaskReq req) {
        scheduledTaskService.editScheduledTask(req);
        return Result.getSuccessResult();
    }

    /**
     * 删除定时任务
     */
    @PostMapping("/remove_by_id")
    @ApiOperation("根据id删除定时任务")
    @CheckPermission("task:write:remove")
    public Result<Void> removeScheduledTaskById(@Valid @RequestBody SearchByIdReq req) {
        scheduledTaskService.removeScheduledTaskById(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 根据id查询定时任务详情
     */
    @OperationLogIgnore
    @PostMapping("/search_by_id")
    @ApiOperation("根据id查询定时任务详情")
    @CheckPermission("task:read")
    public Result<SearchScheduledTaskResp> searchScheduledTaskById(@Valid @RequestBody SearchByIdReq req) {
        return Result.getSuccessResult(scheduledTaskService.searchScheduledTaskById(Long.valueOf(req.getId())));
    }

    /**
     * 分页查询定时任务
     */
    @OperationLogIgnore
    @PostMapping("/page")
    @ApiOperation("分页查询定时任务")
    @CheckPermission("task:read")
    public Result<PaginationResponse<PageScheduledTaskResp>> pageQueryScheduledTask(@Valid @RequestBody PageScheduledTaskReq req) {
        return Result.getSuccessResult(scheduledTaskService.pageQueryScheduledTask(req));
    }

    /**
     * 手动执行一次定时任务
     */
    @PostMapping("/execute")
    @ApiOperation("手动执行一次定时任务")
    @CheckPermission("task:write:execute")
    public Result<Void> doExecuteScheduledTask(@Valid @RequestBody SearchByIdReq req) {
        scheduledTaskService.doExecuteScheduledTask(Long.valueOf(req.getId()));
        return Result.getSuccessResult();
    }

    /**
     * 唤醒定时任务执行器
     */
    @AuthIgnore
    @OperationLogIgnore
    @PostMapping("/notify_executor")
    @ApiOperation("唤醒定时任务执行器")
    public Result<Void> notifyScheduledTaskExecutor() {
        scheduledTaskService.notifyScheduledTaskExecutor();
        return Result.getSuccessResult();
    }

}
